package cache

import (
	"fmt"

	"github.com/BurntSushi/toml"
)

// ----------------------------------------
//  Redis 配置清单（TOML 支持）
// ----------------------------------------
type RedisConfigs struct {
	Items map[string]*RedisConfig `toml:"redis"`
}

// 绑定数据到当前 Redis 配置清单
func (configs *RedisConfigs) Bind(bs []byte) error {
	if err := toml.Unmarshal(bs, configs); err != nil {
		return fmt.Errorf("the redis configs bind failed: %s", err)
	}
	return nil
}

// ----------------------------------------
//  Redis 配置项（TOML 支持, Viper 支持）
// ----------------------------------------
type RedisConfig struct {
	Host         string `toml:"host" mapstructure:"host"`
	Port         int    `toml:"port" mapstructure:"port"`
	Password     string `toml:"password" mapstructure:"password"`
	Database     int    `toml:"database" mapstructure:"database"`
	MaxRetries   int    `toml:"max_retries" mapstructure:"max_retries"`
	PoolSize     int    `toml:"pool_size" mapstructure:"pool_size"`
	Ping         bool   `toml:"ping" mapstructure:"ping"`
	DialTimeout  int    `toml:"dial_timeout" mapstructure:"dial_timeout"`
	ReadTimeout  int    `toml:"read_timeout" mapstructure:"read_timeout"`
	WriteTimeout int    `toml:"write_timeout" mapstructure:"write_timeout"`
}

func (config *RedisConfig) Bind(bs []byte) error {
	if err := toml.Unmarshal(bs, config); err != nil {
		return fmt.Errorf("the redis config bind failed: %s", err)
	}
	return nil
}

// 获取当前 Redis 配置项的副本
func (config *RedisConfig) Copy() *RedisConfig {
	return &RedisConfig{
		Host:         config.Host,
		Port:         config.Port,
		Password:     config.Password,
		Database:     config.Database,
		MaxRetries:   config.MaxRetries,
		PoolSize:     config.PoolSize,
		Ping:         config.Ping,
		DialTimeout:  config.DialTimeout,
		ReadTimeout:  config.ReadTimeout,
		WriteTimeout: config.WriteTimeout,
	}
}
